import Base from 'models/base';
import i18n from 'i18n';

const goodsStatusList = [-2, -1, 0, 1, 2];
const goodsModelStatusList = [0, 1];

export default class GoodsStat extends Base {
  constructor(stat) {
    super();

    let goodsStatusCount = stat.listGoodsDefCount;
    this.setGoodsStatusCount(goodsStatusCount);

    let goodsModelStatusCount = stat.listGoodsModelCount;
    this.setGoodsModelStatusCount(goodsModelStatusCount);

    let goodsModelPriceCount = stat.listGoodsModelPrice[0];
    this.setGoodsModelPriceCount(goodsModelPriceCount);

    let goodsModelPriceArea = stat.listGoodsModelPriceArea;
    this.setGoodsModelPriceArea(goodsModelPriceArea);
  }

  setGoodsStatusCount(goodsStatList) {
    let stat = {};
    for (let i = 0; i < goodsStatList.length; i++) {
      const goodsStat = goodsStatList[i];
      let status = goodsStat.status;
      if (status === -3) {
        continue;
      }
      let statusStr = i18n.getMsg(`goods.goodsDefs.status.${status}`);
      stat[statusStr] = goodsStat.count;
    }

    let goodsStats = [];
    for (let i = 0; i < goodsStatusList.length; i++) {
      let status = goodsStatusList[i];
      let statusStr = i18n.getMsg(`goods.goodsDefs.status.${status}`);
      let count = stat[statusStr];
      if (count === undefined) {
        count = 0;
      }
      goodsStats.push({
        status: statusStr,
        count
      });
    }
    this.goodsStatList = goodsStats;
  }

  setGoodsModelStatusCount(goodsModelStatList) {
    let stat = {};
    for (let i = 0; i < goodsModelStatList.length; i++) {
      const goodsModelStat = goodsModelStatList[i];
      let status = goodsModelStat.status;
      if (status === -1) {
        continue;
      }
      let statusStr = i18n.getMsg(`goods.goodsModel.status.${status}`);
      stat[statusStr] = goodsModelStat.count;
    }

    let goodsModelStats = [];
    for (let i = 0; i < goodsModelStatusList.length; i++) {
      let status = goodsModelStatusList[i];
      let statusStr = i18n.getMsg(`goods.goodsModel.status.${status}`);
      let count = stat[statusStr];
      if (count === undefined) {
        count = 0;
      }
      goodsModelStats.push({
        status: statusStr,
        count
      });
    }
    this.goodsModelStatList = goodsModelStats;
  }

  setGoodsModelPriceCount(goodsModelPriceStat) {
    let dataFieldList = [{
      type: 'costPrice',
      fieldList: ['minGoodsModelCost', 'avgGoodsModelCost', 'maxGoodsModelCost']
    }, {
      type: 'salePrice',
      fieldList: ['minGoodsModelSalePrice', 'avgGoodsModelSalePrice', 'maxGoodsModelSalePrice']
    }, {
      type: 'channelPrice',
      fieldList: ['minGoodsModelChannelPrice', 'avgGoodsModelChannelPrice', 'maxGoodsModelChannelPrice']
    }];

    let goodsModelPrices = [];
    for (let i = 0; i < dataFieldList.length; i++) {
      const dataField = dataFieldList[i];
      let fieldList = dataField.fieldList;
      let dataList = _.map(fieldList, field => {
        return goodsModelPriceStat[field];
      });
      let type = dataField.type;
      goodsModelPrices.push({
        type: i18n.getMsg(`statistic.statChart.goodsModelPriceType.${type}`),
        dataList
      });
    }
    this.goodsModelPriceList = goodsModelPrices;
  }

  setGoodsModelPriceArea(goodsPriceAreaList) {
    let goodsAreaObj = [];
    let fields = ['goodsModelChannelPrice','goodsModelCost','goodsModelSalePrice'];
    let dataFields = ['channelPrice','costPrice','salePrice'];
    let maxPricePre = 0,
      minPrice = 0;
    for (let i = 0; i < goodsPriceAreaList.length; i++) {
      const goodsPriceArea = goodsPriceAreaList[i];
      for (let j = 0; j < fields.length; j++) {
        const field = fields[j];
        let value = goodsPriceArea[field];
        if (maxPricePre < value) {
          maxPricePre = value;
        }
        if (minPrice > value) {
          minPrice = value;
        }
        let dataField = dataFields[j];
        let priceList = goodsAreaObj[dataField];
        if (priceList === undefined) {
          priceList = [];
          goodsAreaObj[dataField] = priceList;
        }
        priceList.push(value);
      }
    }
    this.goodsAreaObj = goodsAreaObj;

    minPrice = _.floor(minPrice, -1);
    let maxPrice = _.ceil(maxPricePre, -1);
    if (maxPrice === 0) {
      maxPrice = 10;
    }

    let intervalPrice = (maxPrice - minPrice) / 15;
    intervalPrice = _.ceil(intervalPrice, -1);
    if (intervalPrice === 0) {
      intervalPrice = 10;
    } else if (intervalPrice > 250) {
      intervalPrice = 250;
    }
    this.areaIntervalPrice = intervalPrice;

    let intervalPriceList = [];
    let price = minPrice;
    while(price <= maxPrice) {
      let obj = {
        minValue:price
      };
      price += intervalPrice;
      obj.maxValue = price;
      obj.text = `${obj.minValue}-${obj.maxValue}`;
      intervalPriceList.push(obj);

      if (price === maxPrice && price !== maxPricePre) {
        break;
      }
    }
    this.intervalPriceList = intervalPriceList;
    this.areaMinPrice = minPrice;
    this.areaMaxPrice = maxPrice;
  }
}
